﻿//生成[Posts]Service代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FineAdmin.IService;
using FineAdmin.Model;
using FineAdmin.IRepository;
using FineAdmin.Common;

namespace FineAdmin.Service
{
    public class PostsService : BaseService<PostsModel>, IPostsService
    {
        public IPostsRepository PostsRepository { get; set; }
        public IEnumerable<PostsModel> GetPostsList()
        {
            return PostsRepository.GetPostsList();
        }


        public dynamic GetListByFilter(PostsModel filter, PageInfo pageInfo)
        {
            pageInfo.prefix = "a.";
            string _where = " Posts a Left JOIN PostCategory b ON a.Post_Type = b.Id Left JOIN ItemsDetail c on a.Business = c.ItemCode";
            _where += " where 1=1";
            if (filter.Post_Type.HasValue)
            {
                _where += string.Format(" and {0}Post_Type = {1}", pageInfo.prefix,filter.Post_Type);
            }
            if (filter.State.HasValue)
            {
                _where += string.Format(" and {0}State = {1}", pageInfo.prefix, filter.State);
            }
            //if (!string.IsNullOrEmpty(filter.ItemName))
            //{
            //    _where += string.Format(" and {0}ItemName=@ItemName", pageInfo.prefix);
            //}
            //if (filter.ItemId != 0)
            //{
            //    _where += string.Format(" and {0}ItemId=@ItemId", pageInfo.prefix);
            //}
            //_where  = CreateTimeWhereStr(filter.StartEndDate, _where, pageInfo.prefix, "CreateTime"); 这句没用
            _where = CreateTimeWhereStr(filter.StartEndDate, _where, pageInfo.prefix);
            pageInfo.returnFields = string.Format("{0}Id,{0}Title,{0}Synopsis,{0}Clicks,{0}Label,{0}Trample,{0}State,b.Name as 'PostTypeName',{0}CreateTime,{0}UpdateTime,{0}Indate,{0}Stime,{0}Link_To,c.ItemName", pageInfo.prefix);
            return GetPageUnite(filter, pageInfo, _where);

            //string _where = " where 1=1";
            //return GetListByFilter(filter, pageInfo, _where);
        }

        public int AddLikes(int id)
        {
            return PostsRepository.AddLikes(id);
        }

        public int AddClicks(int id)
        {
            return PostsRepository.AddClicks(id);
        }

        public int AddTrample(int id)
        {
            return PostsRepository.AddTrample(id);
        }

        public IEnumerable<PostsModel> GetListByFilter(PostsModel filter, PageInfo pageInfo, out int total)
        {
            pageInfo.prefix = "a.";
            string _where = " Posts a Left JOIN PostCategory b ON a.Post_Type = b.Id Left JOIN ItemsDetail c on a.Business = c.ItemCode";
            _where += " where 1=1";
            if (filter.Post_Type.HasValue)
            {
                _where += string.Format(" and {0}Post_Type = {1}", pageInfo.prefix, filter.Post_Type);
            }
            if (filter.State.HasValue)
            {
                _where += string.Format(" and {0}State = {1}", pageInfo.prefix, filter.State);
            }
            //if (!string.IsNullOrEmpty(filter.ItemName))
            //{
            //    _where += string.Format(" and {0}ItemName=@ItemName", pageInfo.prefix);
            //}
            //if (filter.ItemId != 0)
            //{
            //    _where += string.Format(" and {0}ItemId=@ItemId", pageInfo.prefix);
            //}
            //_where  = CreateTimeWhereStr(filter.StartEndDate, _where, pageInfo.prefix, "CreateTime"); 这句没用
            _where = CreateTimeWhereStr(filter.StartEndDate, _where, pageInfo.prefix);
            pageInfo.returnFields = string.Format("{0}Id,{0}Title,{0}Synopsis,{0}Clicks,{0}Label,{0}Trample,{0}Likes,{0}State,b.Name as 'PostTypeName',{0}CreateTime,{0}UpdateTime,{0}Indate,{0}Stime,{0}Link_To,c.ItemName", pageInfo.prefix);
            return GetPageUnite(filter, pageInfo, out total,_where);
        }
    }

}
